package com.pt25.service.sys.repository.mysql.persistence.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pt25.base.dep.db.DbConstants;
import com.pt25.service.sys.repository.mysql.mapper.AdminUserRoleMapper;
import com.pt25.service.sys.repository.mysql.persistence.AdminUserRoleRepo;
import com.pt25.service.sys.repository.mysql.po.AdminUserRolePo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class AdminUserRoleRepoImpl extends ServiceImpl<AdminUserRoleMapper, AdminUserRolePo> implements AdminUserRoleRepo {
    @Override
    public Set<Long> getRoleIdsByUserId(Long userId) {
        QueryWrapper<AdminUserRolePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AdminUserRolePo::getUserId, userId);
        queryWrapper.lambda().eq(AdminUserRolePo::getDelFlag, DbConstants.DEL_FLAG0);
        List<AdminUserRolePo> list = list(queryWrapper);
        return list.stream().map(AdminUserRolePo::getRoleId).collect(Collectors.toSet());
    }

    @Override
    public void delUserRoleByRoleId(Long roleId) {
        QueryWrapper<AdminUserRolePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AdminUserRolePo::getRoleId, roleId);
        remove(queryWrapper);
    }

    @Override
    public void delUserRoleByUserId(Long userId) {
        QueryWrapper<AdminUserRolePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AdminUserRolePo::getUserId, userId);
        remove(queryWrapper);
    }
}
